package com.goldcn.orderborrow.controller;

import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.config.ErpBorrowInterfaceCfg;
import com.goldcn.config.ProConstants;
import com.goldcn.order.model.OrderLogistics;
import com.goldcn.order.service.OrderService;
import com.goldcn.orderborrow.dao.OrderBorrowItemMapper;
import com.goldcn.orderborrow.dao.OrderInfoBorrowMapper;
import com.goldcn.orderborrow.dto.AddOrderBorrowDto;
import com.goldcn.orderborrow.dto.CopyOrDirectBorrowDto;
import com.goldcn.orderborrow.dto.OrderBorrowSelectDto;
import com.goldcn.orderborrow.dto.OrderBorrowStateDto;
import com.goldcn.orderborrow.service.OrderBorrowAuditService;
import com.goldcn.orderborrow.service.OrderBorrowService;
import com.goldcn.orderborrow.vo.OrderBorrowAuditVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Tim Peng
 * @version V1.0
 * @Description:借货控制器
 * @date 2017-4-19 18:20:13
 */
@Api(value = "借货管理", description = "借货订单微服务")
@RestController
@RequestMapping(value = "/orderborrow")
public class OrderBorrowController {
    @Autowired
    OrderBorrowService orderBorrowService;

    @Autowired
    OrderBorrowItemMapper orderBorrowItemMapper;

    @Autowired
    OrderInfoBorrowMapper orderInfoBorrowMapper;

    @Autowired
    OrderBorrowAuditService orderBorrowAuditService;

    @Autowired
    ErpBorrowInterfaceCfg erpBorrowInterfaceCfg;

    @Autowired
    OrderService orderService;

    private Logger logger = Logger.getLogger(OrderBorrowController.class);

    @ApiOperation(value = "新增借货订单")
    @RequestMapping(method = RequestMethod.POST)
    public Object addOrder(@RequestBody List<AddOrderBorrowDto> addOrderDtoList) throws Exception {
        return orderBorrowService.addOrder(addOrderDtoList);
    }

    @ApiOperation(value = "再次借货/立即借货")
    @RequestMapping(value = "/copyOrDirectBorrow", method = RequestMethod.POST)
    public Object copyOrder(@RequestBody CopyOrDirectBorrowDto buyDto) {
        return orderBorrowService.copyOrDirectBorrow(buyDto);
    }

    @ApiOperation(value = "借货订单详情-后台")
    @RequestMapping(value = "/{orderID}", method = RequestMethod.GET)
    public Object selectByOrderID(@PathVariable Long orderID) throws Exception {
        return orderBorrowService.selectByOrderID(orderID, 1l, 1);
    }

    @ApiOperation(value = "借货订单详情-用户")
    @RequestMapping(value = "detail/{orderID}/{userID}", method = RequestMethod.GET)
    public Object selectDetailByOrderID(@PathVariable Long orderID, @PathVariable Long userID) throws Exception {
        return orderBorrowService.selectByOrderID(orderID, userID, 2);
    }

    @ApiOperation(value = "取消借货订单")
    @RequestMapping(value = "/cancel/{orderID}", method = RequestMethod.GET)
    public Object cancelOrder(@PathVariable Long orderID) {
        OrderBorrowStateDto orderStateDto = new OrderBorrowStateDto();
        orderStateDto.setId(orderID);
        orderStateDto.setState(0);
        orderBorrowService.updateState(orderStateDto);
        return "success";
    }

    @ApiOperation(value = "用户借货列表")
    @RequestMapping(value = "/list/{userID}/{state}", method = RequestMethod.GET)
    public Object userOrderBorrowList(@PathVariable Long userID, @PathVariable int state) throws Exception {
        return orderBorrowService.selectOrderBorrowListByUser(userID, state);
    }

    @ApiOperation(value = "定时任务;20小时借货失效")
    @RequestMapping(value = "/timeTaskBorrowOrderCancel", method = RequestMethod.GET)
    public Object timeTaskBorrowOrderCancel() {
        return orderBorrowService.timeTaskOrderCancel();
    }


    @ApiOperation(value = "定时任务;发货超过7天则自动确认")
    @RequestMapping(value = "/timetaskBorrowOrderReceipt", method = RequestMethod.GET)
    public Object timetaskBorrowOrderReceipt() {
        return orderBorrowService.timetaskOrderReceipt();
    }

    @ApiOperation(value = "启动 定时任务;发货超过7天则自动确认")
    @RequestMapping(value = "/setupBorrowOrderReceipt", method = RequestMethod.GET)
    public Object setupBorrowOrderReceipt() {
        return orderBorrowService.setupBorrowOrderReceipt();
    }


    @ApiOperation(value = "添加借货物流运单号[需传物流公司官网URL]")
    @RequestMapping(value = "/logistics", method = RequestMethod.POST)
    public Object addLogistics(@RequestBody OrderLogistics logistics) {
        if(orderService.isOpenErp()){
            throw new ServiceException(ProConstants.TO_ERP_TITLE, RestConst.ErrorCode.FORM_FAIL);
        }
        return orderBorrowService.addLogistics(logistics);
    }

    @ApiOperation(value = "借货订单发货")
    @RequestMapping(value = "/send/{orderID}", method = RequestMethod.GET)
    public Object orderSend(@PathVariable Long orderID) {
        if(orderService.isOpenErp()){
            throw new ServiceException(ProConstants.TO_ERP_TITLE, RestConst.ErrorCode.FORM_FAIL);
        }
        OrderBorrowStateDto orderStateDto = new OrderBorrowStateDto();
        orderStateDto.setId(orderID);
        orderStateDto.setState(5);
        orderBorrowService.updateState(orderStateDto);
        return "success";
    }


    @ApiOperation(value = "借货确认收货")
    @RequestMapping(value = "/receipt/{orderID}", method = RequestMethod.GET)
    public Object orderReceipt(@PathVariable Long orderID) {
        OrderBorrowStateDto orderStateDto = new OrderBorrowStateDto();
        orderStateDto.setId(orderID);
        orderStateDto.setState(6);
        orderStateDto.setTakeType(1L);//本人确认
        orderBorrowService.updateState(orderStateDto);
        return "success";
    }


    @ApiOperation(value = "借货归还")
    @RequestMapping(value = "/giveback", method = RequestMethod.POST)
    public Object giveBack(@RequestBody OrderBorrowStateDto orderBorrowStateDto) {
        if(orderService.isOpenErp()){
            throw new ServiceException(ProConstants.TO_ERP_TITLE, RestConst.ErrorCode.FORM_FAIL);
        }
        orderBorrowStateDto.setState(7);
        orderBorrowService.updateState(orderBorrowStateDto);
        return "success";
    }

    @ApiOperation(value = "借货赠送")
    @RequestMapping(value = "/donate", method = RequestMethod.POST)
    public Object donate(@RequestBody OrderBorrowStateDto orderBorrowStateDto) {
        if(orderService.isOpenErp()){
            throw new ServiceException(ProConstants.TO_ERP_TITLE, RestConst.ErrorCode.FORM_FAIL);
        }
        orderBorrowStateDto.setState(8);
        orderBorrowService.updateState(orderBorrowStateDto);
        return "success";
    }

    @ApiOperation(value = "借货销售")
    @RequestMapping(value = "/sale", method = RequestMethod.POST)
    public Object sale(@RequestBody OrderBorrowStateDto orderBorrowStateDto) {
        if(orderService.isOpenErp()){
            throw new ServiceException(ProConstants.TO_ERP_TITLE, RestConst.ErrorCode.FORM_FAIL);
        }
        orderBorrowStateDto.setState(9);
        orderBorrowService.updateState(orderBorrowStateDto);
        return "success";
    }

    @ApiOperation(value="借货部门审核(批量)")
    @RequestMapping(value = "/AuditStepOne",method = RequestMethod.POST)
    public Object auditStepOne(@RequestBody List<OrderBorrowAuditVo> orderBorrowAuditVoList) throws Exception {
        return orderBorrowAuditService.borrowAuditStepOne(orderBorrowAuditVoList);
    }

    @ApiOperation(value="借货系统审核(批量)")
    @RequestMapping(value = "/AuditStepTwo",method = RequestMethod.POST)
    public Object auditStepTwo(@RequestBody  List<OrderBorrowAuditVo> orderBorrowAuditVoList) throws Exception {
        if(orderService.isOpenErp()){
            throw new ServiceException(ProConstants.TO_ERP_TITLE, RestConst.ErrorCode.FORM_FAIL);
        }
        return orderBorrowAuditService.borrowAuditStepTwo(orderBorrowAuditVoList);
    }

    @ApiOperation(value="借货审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="pageNum",required=true,value="当前页",defaultValue="1"),
            @ApiImplicitParam(paramType="path",name="pageSize",required=true,value="分页大小",defaultValue="5"),
    })
    @RequestMapping(value = "/borrowAuditLis/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Object borrowAuditList(@RequestBody OrderBorrowSelectDto dto,
                                  @PathVariable Integer pageNum,@PathVariable Integer pageSize) throws Exception {
        return orderBorrowService.selectOrderBorrowAuditList(dto,pageNum,pageSize);
    }

    @ApiOperation(value="借货管理;state:31.未发货,5.已发货6.已收货7.已归还8.已赠送")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="pageNum",required=true,value="当前页",defaultValue="1"),
            @ApiImplicitParam(paramType="path",name="pageSize",required=true,value="分页大小",defaultValue="5"),
    })
    @RequestMapping(value = "/orderBorrowList/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Object orderBorrowList(@RequestBody OrderBorrowSelectDto dto,
                                  @PathVariable Integer pageNum,@PathVariable Integer pageSize) throws Exception {
        return orderBorrowService.selectOrderBorrowList(dto,pageNum,pageSize);
    }


    /*####################TO ERP###################*/
    @ApiOperation(value = "ERP订单查询")
    @RequestMapping(value = "/selectByErp/{orderID}", method = RequestMethod.GET)
    public Object selectOrderBorrowErp(@PathVariable Long orderID) {
        return orderBorrowService.selectOrderBorrowErp(orderID);
    }

    @ApiOperation(value="借货系统审核(For ERP)")
    @RequestMapping(value = "/borrowAuditForERP",method = RequestMethod.POST)
    public Object borrowAuditForERP(@RequestBody  OrderBorrowAuditVo orderBorrowAuditVoList) throws Exception {
        return orderBorrowAuditService.borrowAuditForERP(orderBorrowAuditVoList);
    }

    @ApiOperation(value = "ERP添加快递单号以及发货")
    @RequestMapping(value = "/sendForERP", method = RequestMethod.POST)
    public Object sendForERP(@RequestBody OrderLogistics logistics) {
        return orderBorrowService.addLogsAndSendForERP(logistics);
    }

    @ApiOperation(value = "ERP撤回发货")
    @RequestMapping(value = "/sendBackForERP/{orderID}", method = RequestMethod.GET)
    public Object sendBackForERP(@PathVariable Long orderID) {
        return orderBorrowService.sendBackForERP(orderID);
    }

    @ApiOperation(value = "ERP借货归还:state; 7归还,8赠送,9销售")
    @RequestMapping(value = "/giveBackForERP", method = RequestMethod.POST)
    public Object giveBackForERP(@RequestBody OrderBorrowStateDto orderBorrowStateDto) {
        return orderBorrowService.giveBackForERP(orderBorrowStateDto);
    }

    @ApiOperation(value = "查询订单套账信息")
    @RequestMapping(value = "/erp/{orderID}", method = RequestMethod.GET)
    public Object selCompanyById(@PathVariable Long orderID) throws Exception {
        return orderBorrowService.selCompanyById(orderID);
    }


}
